'''
Created on Apr 29, 2014

@author: root
'''
import uuid

from wm.etc.config import ASCONFIG
from wm.lib.aisec import AScanner
from wm.lib.loglib import log_error
from wm.lib import monitorlib
from wm.db.TransactionManager import TransactionManager

def vulscaning(websiteid, url):
    print '---in0----'
    tran = TransactionManager()
    try:
        tran.startTransaction()
        print '---in1----'
        scanner = AScanner(ASCONFIG['USERNAME'],ASCONFIG['PASSWORD'])
        print '---in2----'
        if scanner:
            print '---in3----'
            task = scanner.addTask(url, ASCONFIG['CALLBACK'])
            print task,url
            if task[0] == '1':
                vstid = uuid.uuid1().__str__().replace("-","") 
                sql = "insert into vulscantask(id,createDate,modifyDate,entry,callback,taskID,websiteConfigInfo_id) "+\
                        "values('" + vstid + "'," + " CURRENT_TIMESTAMP()," + " CURRENT_TIMESTAMP()," +\
                                    "'"+ url + "','" + ASCONFIG['CALLBACK'] + "','" + str(task[1])+ "','" + websiteid + "')"                
                tran.execute(sql)
                
        tran.commitTransaction()
    except Exception as e:
        log_error("vulscaning():"+str(e))
        tran.rollbackTransaction()
    finally:
        tran.close()
        
if __name__=="__main__":
    print '----------start--------------------------'
    sitelist = monitorlib.get_website_list(3)    
    print sitelist
           
    if sitelist:
        for row in sitelist:
            vulscaning(row[0],row[1])

